package com.ctrip.ec.api.admin.controller.topic;
import com.ctrip.ec.api.admin.model.query.QueryModel;
import com.ctrip.ec.api.admin.model.topic.TopicModel;
import com.ctrip.ec.biz.dao.topic.TopicContentDao;
import com.ctrip.ec.biz.entity.topic.TopicContentEntity;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.topic.TopicDao;
import com.ctrip.ec.biz.entity.topic.TopicEntity;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("topic")
public class TopicController{
    @Autowired
    private TopicDao topic;

    @Autowired
    private TopicContentDao topicContent;

    @Value("${imageDomain}")
    private  String imageDomain;

    @PostMapping("list")
    public List<TopicEntity> list(@RequestBody QueryModel model) throws  SQLException{
        PagerSetting setting = model.build();
        List<TopicEntity> list = topic.list(setting);

//        List<TopicModel> modelList = new ArrayList<>();
//        for(TopicEntity entity :list){
//            TopicModel topicModel = new TopicModel(entity);
//            topicModel.setContentList(topicContent.list("topic_id=" + entity.getId()));
//            modelList.add(topicModel);
//        }
        RequestContext.setResponseHeaderCount(setting.getRecordCount());
        return list;
    }

    @GetMapping("detail/{id}")
    public  TopicModel detail(@PathVariable Long id) throws SQLException {
        TopicModel model = new TopicModel(topic.entity(id));
        List<TopicContentEntity> list  = topicContent.list("topic_id=" + id);

        model.setContentList(list);
        return model;
    }

    @PostMapping("save")
    public  Long save(@RequestBody TopicModel model) throws SQLException {
        model.setUserId(RequestContext.getUserId());
        model.setUserName(RequestContext.getUserName());
        topic.save(model);
        if(model.getContentList()!=null && model.getContentList().size()>0){

            //先删除不用的图片
            List<TopicContentEntity> oldList = topicContent.listByTopicId(model.getId());
            List<Long> newImageIds = model.getContentList().stream().filter(t->t.getId()!=null && t.getId()!=0).map(t->t.getImageId()).collect(Collectors.toList());

            for(TopicContentEntity topicContentEntity: oldList){
                if(!newImageIds.contains(topicContentEntity.getImageId())){//没有就删除
                    topicContent.delete(topicContentEntity.getId());

                }
            }
            //老的里面没有的就是需要添加的
            List<Long> oldImageIds = oldList.stream().map(t->t.getImageId()).collect(Collectors.toList());
            for(TopicContentEntity topicContentEntity: model.getContentList()){
                if(!oldImageIds.contains(topicContentEntity.getImageId())) {
                    topicContentEntity.setTopicId(model.getId());
                    topicContent.save(topicContentEntity);
                }
            }


        }

        return model.getId();
    }

    @PostMapping("delete")
    public  void delete(@RequestBody List<Long> ids) throws SQLException {
        topic.delete(ids);
    }
    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws  SQLException{
        topic.erase(ids);
    }

    @PostMapping("unerase")
    public  void unerase(@RequestBody List<Long> ids) throws  SQLException{
        topic.unerase(ids);
    }
}
